package ElevatorPackage;

import org.junit.Before;
import org.junit.Test;

import java.util.*;

import static org.junit.Assert.*;

/**
 * Created by Connor Madden.
 * User: Connor Madden
 * Date: 16/08/11
 * Time: 7:47 PM
 */
public class ElevatorTest {

    private static final int LOCATION = 0;
    private static final int DESTINATION = 10;
    private static final int STEP_TIME = 200;
    private static final int EXCHANGE_TIME = 500;
    private static final int TEST_INT = 5;
    private static final int DIRECTION_UP = 1;
    private static final int DIRECTION_DOWN = -1;

    Elevator elevator;

    @Before
    public void setUp() throws Exception {
        elevator = new Elevator(LOCATION, STEP_TIME, EXCHANGE_TIME);
    }

    @Test
    public void testGetDisembarkLocations() throws Exception {
        LinkedList<Integer> l = new LinkedList<Integer>();
        l.add(TEST_INT);
        elevator.disembarkLocations = l;
        assertEquals(elevator.getDisembarkLocations(), l);
    }

    @Test
    public void testGetActiveCalls() throws Exception {
        TreeMap<Integer, ArrayList<Integer>> t = new TreeMap<Integer, ArrayList<Integer>>();
        ArrayList<Integer> l = new ArrayList<Integer>(2);
        l.add(DIRECTION_UP);
        l.add(DIRECTION_DOWN);
        t.put(LOCATION, l);
        elevator.activeCalls = t;
        assertTrue(elevator.getActiveCalls().containsKey(LOCATION));
        assertTrue(elevator.getActiveCalls().get(LOCATION).contains(DIRECTION_UP) && elevator.getActiveCalls().get(LOCATION).contains(DIRECTION_DOWN));

    }

    @Test
    public void testGetStepTime() throws Exception {
        elevator.stepTime = STEP_TIME + 1;
        assertEquals(elevator.getStepTime(), STEP_TIME + 1);
    }

    @Test
    public void testSetStepTime() throws Exception {
        elevator.setStepTime(STEP_TIME + 1);
        assertEquals(elevator.getStepTime(), STEP_TIME + 1);
    }

    @Test
    public void testGetExchangeTime() throws Exception {
        elevator.exchangeTime = EXCHANGE_TIME + 1;
        assertEquals(elevator.getExchangeTime(), EXCHANGE_TIME + 1);
    }

    @Test
    public void testSetExchangeTime() throws Exception {
        elevator.setExchangeTime(EXCHANGE_TIME + 1);
        assertEquals(elevator.getExchangeTime(), EXCHANGE_TIME + 1);
    }

    @Test
    public void testIsHasDestination() throws Exception {
        elevator.hasDestination = true;
        assertTrue(elevator.isHasDestination());
    }

    @Test
    public void testGetDestination() throws Exception {
        elevator.destination = DESTINATION + 1;
        assertEquals(elevator.getDestination(), DESTINATION + 1);
    }

    @Test
    public void testGetLocation() throws Exception {
        elevator.location = LOCATION + 1;
        assertEquals(elevator.getLocation(), LOCATION + 1);
    }

    @Test
    public void testGetPrevLocation() throws Exception {
        elevator.prevLocation = LOCATION + 1;
        assertEquals(elevator.getPrevLocation(), LOCATION + 1);
    }

    @Test
    public void testGetStatus() throws Exception {
        elevator.status = Elevator.Status.WAITING;
        assertEquals(elevator.getStatus(), Elevator.Status.WAITING);
    }

    @Test
    public void testRun() throws Exception {
        elevator.addObserver(new Observer() {
            public void update(Observable o, Object arg) {
                if (arg == Elevator.ElevatorAction.GET_DESTINATION)
                    System.out.println("Getting Destination: " + elevator.getDestination());
                if (arg == Elevator.ElevatorAction.WAITING)
                    System.out.println("Waiting");
                if (arg == Elevator.ElevatorAction.MOVE)
                    System.out.println("Moving: From " + elevator.getLocation() + " To " + (elevator.getLocation() + 1));
                if (arg == Elevator.ElevatorAction.ARRIVE_AT_FLOOR)
                    System.out.println("Arrived at Floor: " + elevator.getLocation());
                if (arg == Elevator.ElevatorAction.ARRIVE_AT_DESTINATION)
                    System.out.println("Arrived at Destination: " + elevator.getLocation());
                if (arg == Elevator.ElevatorAction.STOP)
                    System.out.println("Stopping");
                if (arg == Elevator.ElevatorAction.OPEN_DOOR)
                    System.out.println("Opening Door");
                if (arg == Elevator.ElevatorAction.CLOSE_DOOR)
                    System.out.println("Closing Door");
                if (arg == Elevator.ElevatorAction.DISEMBARK_PASSENGERS)
                    System.out.println("Disembarking Passengers");
                if (arg == Elevator.ElevatorAction.DEPART_FLOOR)
                    System.out.println("Disembarking Floor");
            }
        }
        );
        elevator.addDisembarkLocation(TEST_INT);
    }

    @Test
    public void testAddDisembarkLocation() throws Exception {
//        elevator.addDisembarkLocation(TEST_INT);
//        assertTrue("disembarkLocation.toString(): " + elevator.disembarkLocations.toString(), elevator.disembarkLocations.contains(TEST_INT));
    }

    @Test
    public void testActivateCall() throws Exception {
//        elevator.activateCall(LOCATION, DIRECTION_DOWN);
//        elevator.activateCall(LOCATION, DIRECTION_DOWN);
//        elevator.activateCall(LOCATION, DIRECTION_UP);
//        elevator.activateCall(LOCATION + 1, DIRECTION_DOWN);
//        elevator.activateCall(LOCATION + 1, DIRECTION_UP);
//
//        assertTrue(elevator.getActiveCalls().containsKey(LOCATION));
//        assertTrue(elevator.getActiveCalls().get(LOCATION).contains(DIRECTION_UP) && elevator.getActiveCalls().get(LOCATION).contains(DIRECTION_DOWN));
//        assertTrue(elevator.getActiveCalls().containsKey(LOCATION + 1));
//        assertTrue(elevator.getActiveCalls().get(LOCATION).contains(DIRECTION_UP) && elevator.getActiveCalls().get(LOCATION).contains(DIRECTION_DOWN));
    }
}
